import asyncio
import yaml
import json
from typing import List, Dict
from langchain_core.prompts import ChatPromptTemplate
from tools.log_handler import logger
from config.PathConfig import PROMPTS_CONFIG

# 用户 user   系统role
# 对话管理
class SessionManager:
    def __init__(self, default_role: str = "小李"):
        self.messages: Dict[str, List[Dict[str, str]]] = {}
        self.current_role: Dict[str, str] = {}
        self.default_role = default_role
    
    def init_session(self, user: str):
        if user not in self.messages:
            self.messages[user] = []
            self.current_role[user] = self.default_role
    
    def append_message(self, user: str, role: str, content: str):
        self.messages[user].append({"role": role, "content": content})
    
    def get_history(self, user: str) -> List[Dict[str, str]]:
        return self.messages[user]
    
    def switch_role(self, user: str, new_role: str, prompts: Dict) -> str:
        if new_role in prompts:
            self.current_role[user] = new_role
            self.messages[user] = []
            return f"已切换到 {new_role}，你好呀"
        return "角色不存在"